home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / MiscKit1.7.1 / MiscKit / Palettes / MiscDragViews / MiscViews.subproj / MiscImageDragView.m < prev    next >
Encoding:
Text File  |  1995-04-12  |  4.2 KB  |  213 lines

  1. /**************************************************************************
  2.  * CLASS:        MiscImageDragView
  3.  * Copyright (C) 1995 Robert Todd Thomas
  4.  * Use is governed by the MiscKit license
  5.  *
  6.  *    See the header file for more information on this class.
  7.  **************************************************************************/
  8.  
  9. #import <appkit/Pasteboard.h> 
  10. #import "MiscImageDragView.h"
  11.  
  12. #define MISC_IMAGEDRAGVIEW_VERSION 1
  13.  
  14.  
  15. @implementation MiscImageDragView
  16.  
  17. + initialize
  18. {
  19.     // Set the versioning for archiving.
  20.     if (self == [MiscImageDragView class])
  21.         [self setVersion: MISC_IMAGEDRAGVIEW_VERSION];
  22.     
  23.     return self;
  24. }
  25.  
  26.  
  27.  
  28. // Register to accept dragging, and setup some defaults.
  29.  
  30. - initFrame: (NXRect *)frameRect
  31. {    
  32.     [super initFrame: frameRect];
  33.     [self setDragIconRepresentation: YES];
  34.  
  35.     return self;
  36. }
  37.  
  38.  
  39.  
  40. - initDragTypes
  41. {
  42.   const char *const types[] = { NXPostScriptPboardType, NXTIFFPboardType,     
  43.                                   NXFilenamePboardType };
  44.  
  45.     [self registerForDraggedTypes: (const char *const *)&types count: 3];
  46.     return self;
  47. }
  48.  
  49.  
  50.  
  51. //  Sets whether the TIFF itself or its TIFF icon representation is dragged.
  52.  
  53. - setDragIconRepresentation: (BOOL)aBool
  54. {
  55.     dragIconRepresentation = aBool;
  56.     return self;
  57. }
  58.  
  59.  
  60.  
  61. - (BOOL)dragIconRepresentation
  62. {
  63.     return dragIconRepresentation;
  64. }
  65.  
  66.  
  67.  
  68. // Put the image data on the NXTIFFPboardType. Also set the image that is
  69. // going to be used for the drag, depending upon whether 
  70. // dragIconRepresentation is true.
  71.  
  72. - (BOOL)setupForSourceDrag
  73. {
  74.   id  dragPB = [self draggingPasteboard];
  75.   NXStream  *imageStream = NXOpenMemory (NULL, 0, NX_WRITEONLY);
  76.   const char *const types[] = { NXTIFFPboardType };
  77.     
  78.     // write the NXImage in TIFF form (it's original size)
  79.     
  80.     [dragPB declareTypes: types num: 1 owner: self];
  81.     
  82.     // put the image back to regular size and write it
  83.     
  84.     [ [self image] setSize: &imageSize];
  85.     [ [self image] writeTIFF: imageStream];
  86.     [dragPB writeType: NXTIFFPboardType fromStream: imageStream];
  87.  
  88.     NXCloseMemory (imageStream, NX_FREEBUFFER);        
  89.  
  90.     // if use icon rep. grab the image from somewhere that everyone has
  91.     
  92.     if ([self dragIconRepresentation])
  93.         [self setDragImage:  [ [NXImage alloc] initFromFile:     
  94.                 "/NextApps/Preview.app/tiff.tiff"] freeWhenDone: YES];    
  95.     else
  96.         [self setDragImage: [self image] ];    
  97.  
  98.     return YES;
  99. }
  100.  
  101.  
  102.  
  103. // Adjust the dragPoint so that you grab the image in the center.
  104.  
  105. - calculateDragPoint: (NXPoint *)dragPoint andOffset: (NXPoint *)offset
  106. {
  107.     if ([self dragIconRepresentation])
  108.     {
  109.         dragPoint->x -= 24.0;
  110.         dragPoint->y -= 24.0;
  111.      }
  112.     else
  113.     {
  114.       NXSize  imSize;
  115.       
  116.           [ [self image] getSize: &imSize];
  117.         dragPoint->x -= imSize.width / 2.0;
  118.         dragPoint->y -= imSize.height / 2.0;
  119.      }
  120.      
  121.     return self;
  122. }
  123.  
  124.  
  125.  
  126. // This sure is a complicated method. Actually NXImage takes care
  127. // of everything. It checks for filenames, correct extensions, data
  128. // on the correct pasteboards, and even accepts non native images if 
  129. // you have the filters.
  130.         
  131. - (BOOL)prepareForDragOperation: sender
  132. {
  133.     if ([NXImage canInitFromPasteboard: [sender draggingPasteboard] ])
  134.         return YES;
  135.  
  136.     [self cleanupAfterDestinationDrag];
  137.     [self display];
  138.     
  139.     return NO;
  140. }
  141.  
  142.  
  143.  
  144. // Take the pasteboard data and create the image.
  145.  
  146. - (BOOL)performDragOperation: sender
  147. {
  148.     [self setImage: [ [NXImage alloc] initFromPasteboard: 
  149.             [sender draggingPasteboard] ] ];
  150.  
  151.     return YES;
  152. }
  153.  
  154.  
  155.  
  156. // Archiving methods
  157.  
  158. - read: (NXTypedStream *)stream
  159. {
  160.     int  version;
  161.     
  162.     [super read: stream];
  163.     
  164.     version = NXTypedStreamClassVersion(stream, "MiscImageDragView");
  165.     switch (version)
  166.     {
  167.         case 0:
  168.         break;
  169.         
  170.         case MISC_IMAGEDRAGVIEW_VERSION:
  171.             NXReadType (stream, "c", &dragIconRepresentation);
  172.         break;
  173.      }
  174.          
  175.     return self;
  176. }
  177.  
  178.  
  179.  
  180. - write: (NXTypedStream *)stream
  181. {
  182.     [super write: stream];
  183.     NXWriteType (stream, "c", &dragIconRepresentation);
  184.  
  185.     return self;
  186. }
  187.  
  188.  
  189.  
  190. - (const char *)stringValue
  191. {    // we don't currently remember the filename, so we don't
  192.     // know where the image came from...
  193.     return [super stringValue];
  194. }
  195.  
  196.  
  197.  
  198. - setStringValue:(const char *)aValue
  199. {
  200.     return [self setImageByFilename:aValue];
  201. }
  202.  
  203. @end
  204.  
  205.  
  206. /*****************************************************************************
  207.   CHANGES:
  208.       Version 0.4, November 30, 1994 (Todd)
  209.         1) Just made it compatible with the changes made in MiscDragView and
  210.             added archive versioning.
  211.  
  212.  *****************************************************************************/
  213.